• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /macosx-10.10.1/pyobjc-45/2.6/pyobjc/pyobjc-framework-Quartz/Examples/Programming with Quartz/BasicDrawing/
1import sys
2
3from Quartz import *
4
5import Utilities
6import array
7
8def doColorSpaceFillAndStroke(context):
9	theColorSpace = Utilities.getTheCalibratedRGBColorSpace()
10	opaqueRed = ( 0.663, 0.0, 0.031, 1.0 ) # red,green,blue,alpha
11	aBlue = ( 0.482, 0.62, 0.871, 1.0 )	# red,green,blue,alpha
12
13	# Set the fill color space to be the generic calibrated RGB color space.
14	CGContextSetFillColorSpace(context, theColorSpace)
15	# Set the fill color to opaque red. The number of elements in the
16	# array passed to this function must be the number of color
17	# components in the current fill color space plus 1 for alpha.
18	CGContextSetFillColor(context, opaqueRed)
19
20	# Set the stroke color space to be the generic calibrated RGB color space.
21	CGContextSetStrokeColorSpace(context, theColorSpace)
22	# Set the stroke color to opaque blue. The number of elements
23	# in the array passed to this function must be the number of color
24	# components in the current stroke color space plus 1 for alpha.
25	CGContextSetStrokeColor(context, aBlue)
26
27	CGContextSetLineWidth(context, 8.0)
28 	# Rectangle 1.
29	CGContextBeginPath(context)
30	CGContextAddRect(context, CGRectMake(20.0, 20.0, 100.0, 100.0))
31	CGContextDrawPath(context, kCGPathFillStroke)
32
33	# Continue to use the stroke colorspace already set
34	# but change the stroke alpha value to a semitransparent blue.
35        aBlue = list(aBlue)
36	aBlue[3] = 0.5
37	CGContextSetStrokeColor(context, aBlue)
38 	# Rectangle 2.
39	CGContextBeginPath(context)
40	CGContextAddRect(context, CGRectMake(140.0, 20.0, 100.0, 100.0))
41	CGContextDrawPath(context, kCGPathFillStroke)
42
43	# Don't release the color space since this routine
44	# didn't create it.
45
46_opaqueRedColor = None
47_opaqueBlueColor = None
48_transparentBlueColor = None
49
50def drawWithColorRefs(context):
51        global _opaqueRedColor
52        global _opaqueBlueColor
53        global _transparentBlueColor
54
55	# Initialize the CGColorRefs if necessary
56        if _opaqueRedColor is None:
57		# Initialize the color array to an opaque red
58		# in the generic calibrated RGB color space.
59		color = (0.663, 0.0, 0.031, 1.0)
60		theColorSpace = Utilities.getTheCalibratedRGBColorSpace()
61		# Create a CGColorRef for opaque red.
62		_opaqueRedColor = CGColorCreate(theColorSpace, color)
63		# Make the color array correspond to an opaque blue color.
64                color = (0.482, 0.62, 0.87, 1.0)
65		# Create another CGColorRef for opaque blue.
66		_opaqueBlueColor = CGColorCreate(theColorSpace, color)
67		# Create a new CGColorRef from the opaqueBlue CGColorRef
68		# but with a different alpha value.
69		_transparentBlueColor = CGColorCreateCopyWithAlpha(
70                        _opaqueBlueColor, 0.5)
71                if _opaqueRedColor is None or _opaqueBlueColor is None or _transparentBlueColor is None:
72		    print >>sys.stderr, "Couldn't create one of the CGColorRefs!!!"
73		    return
74
75	# Set the fill color to the opaque red CGColor object.
76	CGContextSetFillColorWithColor(context, _opaqueRedColor)
77	# Set the stroke color to the opaque blue CGColor object.
78	CGContextSetStrokeColorWithColor(context, _opaqueBlueColor)
79
80	CGContextSetLineWidth(context, 8.0)
81	# Draw the first rectangle.
82	CGContextBeginPath(context)
83	CGContextAddRect(context, CGRectMake(20.0, 20.0, 100.0, 100.0))
84	CGContextDrawPath(context, kCGPathFillStroke)
85
86	# Set the stroke color to be that of the transparent blue
87	# CGColor object.
88	CGContextSetStrokeColorWithColor(context, _transparentBlueColor)
89	# Draw a second rectangle to the right of the first one.
90	CGContextBeginPath(context)
91 	CGContextAddRect(context, CGRectMake(140.0, 20.0, 100.0, 100.0))
92	CGContextDrawPath(context, kCGPathFillStroke)
93
94def doIndexedColorDrawGraphics(context):
95	theBaseRGBSpace = Utilities.getTheCalibratedRGBColorSpace()
96	lookupTable = array.array('B', (0,)*6)
97	opaqueRed = (0, 1) # index, alpha
98	aBlue = (1, 1)     # index, alpha
99
100	# Set the first 3 values in the lookup table to a red of
101	# 169/255 = 0.663, no green, and blue = 8/255 = 0.031. This makes
102	# the first entry in the lookup table a shade of red.
103	lookupTable[0] = 169; lookupTable[1] = 0; lookupTable[2] = 8
104
105	# Set the second 3 values in the lookup table to a red value
106	# of 123/255 = 0.482, a green value of 158/255 = 0.62, and
107	# a blue value of 222/255 = 0.871. This makes the second entry
108	# in the lookup table a shade of blue.
109	lookupTable[3] = 123; lookupTable[4] = 158; lookupTable[5] = 222
110
111	# Create the indexed color space with this color lookup table,
112	# using the RGB color space as the base color space and a 2 element
113	# color lookup table to characterize the indexed color space.
114	theIndexedSpace = CGColorSpaceCreateIndexed(theBaseRGBSpace, 1, lookupTable)
115        if theIndexedSpace is not None:
116	    CGContextSetStrokeColorSpace(context, theIndexedSpace)
117	    CGContextSetFillColorSpace(context, theIndexedSpace)
118
119	    # Set the stroke color to an opaque blue.
120	    CGContextSetStrokeColor(context, aBlue)
121	    # Set the fill color to an opaque red.
122	    CGContextSetFillColor(context, opaqueRed)
123
124	    CGContextSetLineWidth(context, 8.0)
125            # Draw the first rectangle.
126	    CGContextBeginPath(context)
127	    CGContextAddRect(context, CGRectMake(20.0, 20.0, 100.0, 100.0))
128	    CGContextDrawPath(context, kCGPathFillStroke)
129
130	    # Continue to use the stroke colorspace already set
131	    # but change the stroke alpha value to a semitransparent value
132	    # while leaving the index value unchanged.
133            aBlue = list(aBlue)
134	    aBlue[1] = 0.5
135	    CGContextSetStrokeColor(context, aBlue)
136	    # Draw another rectangle to the right of the first one.
137	    CGContextBeginPath(context)
138	    CGContextAddRect(context, CGRectMake(140.0, 20.0, 100.0, 100.0))
139	    CGContextDrawPath(context, kCGPathFillStroke)
140        else:
141	    print >>sys.stderr, "Couldn't make the indexed color space!"
142
143def drawWithGlobalAlpha(context):
144	rect = CGRectMake(40.0, 210.0, 100.0, 100.0)
145	color = [1.0, 0.0, 0.0, 1.0] # opaque red
146	# Set the fill color space to that returned by getTheCalibratedRGBColorSpace.
147	CGContextSetFillColorSpace(context, Utilities.getTheCalibratedRGBColorSpace())
148
149	CGContextSetFillColor(context, color)
150        for i in range(2):
151            CGContextSaveGState(context)
152            # Paint the leftmost rect on this row with 100% opaque red.
153            CGContextFillRect(context, rect)
154
155            CGContextTranslateCTM(context, rect.size.width + 70.0, 0.0)
156            # Set the alpha value of this rgba color to 0.5.
157            color[3] = 0.5
158            # Use the new color as the fill color in the graphics state.
159            CGContextSetFillColor(context, color)
160            # Paint the center rect on this row with 50% opaque red.
161            CGContextFillRect(context, rect)
162
163            CGContextTranslateCTM(context, rect.size.width + 70.0, 0.0)
164            # Set the alpha value of this rgba color to 0.25.
165            color[3] = 0.25
166            # Use the new color as the fill color in the graphics state.
167            CGContextSetFillColor(context, color)
168            # Paint the rightmost rect on this row with 25% opaque red.
169            CGContextFillRect(context, rect)
170            CGContextRestoreGState(context)
171            # After restoring the graphics state, the fill color is set to
172            # that prior to calling CGContextSaveGState, that is, opaque
173            # red. The coordinate system is also restored.
174
175            # Now set the context global alpha value to 50% opaque.
176            CGContextSetAlpha(context, 0.5)
177            # Translate down for a second row of rectangles.
178            CGContextTranslateCTM(context, 0.0, -(rect.size.height + 70.0))
179            # Reset the alpha value of the color array to fully opaque.
180            color[3] = 1.0
181
182def drawWithColorBlendMode(context, url):
183    # A pleasant green color.
184    green = [0.584, 0.871, 0.318, 1.0]
185
186    # Create a CGPDFDocument object from the URL.
187    pdfDoc = CGPDFDocumentCreateWithURL(url)
188    if pdfDoc is None:
189        print >>sys.stderr, "Couldn't create CGPDFDocument from URL!"
190        return
191
192    # Obtain the media box for page 1 of the PDF document.
193    pdfRect = CGPDFDocumentGetMediaBox(pdfDoc, 1)
194    # Set the origin of the rectangle to (0,0).
195    pdfRect.origin.x = pdfRect.origin.y = 0
196
197    # Graphic 1, the left portion of the figure.
198    CGContextTranslateCTM(context, 20, 10 + CGRectGetHeight(pdfRect)/2)
199
200    # Draw the PDF document.
201    CGContextDrawPDFDocument(context, pdfRect, pdfDoc, 1)
202
203    # Set the fill color space to that returned by getTheCalibratedRGBColorSpace.
204    CGContextSetFillColorSpace(context, Utilities.getTheCalibratedRGBColorSpace())
205    # Set the fill color to green.
206    CGContextSetFillColor(context, green)
207
208    # Graphic 2, the top-right portion of the figure.
209    CGContextTranslateCTM(context, CGRectGetWidth(pdfRect) + 10,
210				    CGRectGetHeight(pdfRect)/2 + 10)
211
212    # Draw the PDF document again.
213    CGContextDrawPDFDocument(context, pdfRect, pdfDoc, 1)
214
215    # Make a fill rectangle that is the same size as the PDF document
216    # but inset each side by 80 units in x and 20 units in y.
217    insetRect = CGRectInset(pdfRect, 80, 20)
218    # Fill the rectangle with green. Because the fill color is opaque and
219    # the blend mode is Normal, this obscures the drawing underneath.
220    CGContextFillRect(context, insetRect)
221
222    # Graphic 3, the bottom-right portion of the figure.
223    CGContextTranslateCTM(context, 0, -(10 + CGRectGetHeight(pdfRect)))
224
225    # Draw the PDF document again.
226    CGContextDrawPDFDocument(context, pdfRect, pdfDoc, 1)
227
228    # Set the blend mode to kCGBlendModeColor which will
229    # colorize the destination with subsequent drawing.
230    CGContextSetBlendMode(context, kCGBlendModeColor)
231    # Draw the rectangle on top of the PDF document. The portion of the
232    # background that is covered by the rectangle is colorized
233    # with the fill color.
234    CGContextFillRect(context, insetRect)
235
236
237def createEllipsePath(context, center, ellipseSize):
238    CGContextSaveGState(context)
239    # Translate the coordinate origin to the center point.
240    CGContextTranslateCTM(context, center.x, center.y)
241    # Scale the coordinate system to half the width and height
242    # of the ellipse.
243    CGContextScaleCTM(context, ellipseSize.width/2, ellipseSize.height/2)
244    CGContextBeginPath(context)
245    # Add a circular arc to the path, centered at the origin and
246    # with a radius of 1.0. This radius, together with the
247    # scaling above for the width and height, produces an ellipse
248    # of the correct size.
249    CGContextAddArc(context, 0.0, 0.0, 1.0, 0.0, Utilities.DEGREES_TO_RADIANS(360.0), 0.0)
250    # Close the path so that this path is suitable for stroking,
251    # should that be desired.
252    CGContextClosePath(context)
253    CGContextRestoreGState(context)
254
255_opaqueBrownColor = None
256_opaqueOrangeColor = None
257def doClippedEllipse(context):
258    global _opaqueBrownColor, _opaqueOrangeColor
259
260    theCenterPoint = CGPoint(120.0, 120.0)
261    theEllipseSize = CGSize(100.0, 200.0)
262    dash = [ 2.0 ]
263
264    # Initialize the CGColorRefs if necessary.
265    if _opaqueBrownColor is None:
266        # The initial value of the color array is an
267        # opaque brown in an RGB color space.
268        color = [0.325, 0.208, 0.157, 1.0]
269        theColorSpace = Utilities.getTheCalibratedRGBColorSpace()
270        # Create a CGColorRef for opaque brown.
271        _opaqueBrownColor = CGColorCreate(theColorSpace, color)
272        # Make the color array correspond to an opaque orange.
273        color = [0.965, 0.584, 0.059, 1.0 ]
274        # Create another CGColorRef for opaque orange.
275        _opaqueOrangeColor = CGColorCreate(theColorSpace, color)
276
277    # Draw two ellipses centered about the same point, one
278    # rotated 45 degrees from the other.
279    CGContextSaveGState(context)
280    # Ellipse 1
281    createEllipsePath(context, theCenterPoint, theEllipseSize)
282    CGContextSetFillColorWithColor(context, _opaqueBrownColor)
283    CGContextFillPath(context)
284    # Translate and rotate about the center point of the ellipse.
285    CGContextTranslateCTM(context, theCenterPoint.x, theCenterPoint.y)
286    # Rotate by 45 degrees.
287    CGContextRotateCTM(context, Utilities.DEGREES_TO_RADIANS(45))
288    # Ellipse 2
289    # CGPointZero is a pre-defined Quartz point corresponding to
290    # the coordinate (0,0).
291    createEllipsePath(context, CGPointZero, theEllipseSize)
292    CGContextSetFillColorWithColor(context, _opaqueOrangeColor)
293    CGContextFillPath(context)
294    CGContextRestoreGState(context)
295
296    CGContextTranslateCTM(context, 170.0, 0.0)
297    # Now use the first ellipse as a clipping area prior to
298    # painting the second ellipse.
299    CGContextSaveGState(context)
300    # Ellipse 3
301    createEllipsePath(context, theCenterPoint, theEllipseSize)
302    CGContextSetStrokeColorWithColor(context, _opaqueBrownColor)
303    CGContextSetLineDash(context, 0, dash, 1)
304    # Stroke the path with a dash.
305    CGContextStrokePath(context)
306    # Ellipse 4
307    createEllipsePath(context, theCenterPoint, theEllipseSize)
308    # Clip to the elliptical path.
309    CGContextClip(context)
310    CGContextTranslateCTM(context, theCenterPoint.x, theCenterPoint.y)
311    # Rotate by 45 degrees.
312    CGContextRotateCTM(context, Utilities.DEGREES_TO_RADIANS(45))
313    # Ellipse 5
314    createEllipsePath(context, CGPointZero, theEllipseSize)
315    CGContextSetFillColorWithColor(context, _opaqueOrangeColor)
316    CGContextFillPath(context)
317    CGContextRestoreGState(context)
318